/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#ifndef _LYRICSDATA_H_
#define _LYRICSDATA_H_

#include "basictype.h"
#include <vector>

class QTextStream;
class QTextCodec;
class QFile;

/** This class is used to handle lyrics data. */
class LyricsData {
public:
    /** This struct is a single line of lyrics. */
    struct LyricsElement {
        /** Default constructor. */
        LyricsElement(const LyricsTime off = 0, const QString& lyr = QString()) : offset(off), lyrics(lyr) {};
        /** The start time of the lyrics line. */
        LyricsTime offset;
        /** The text of the lyrics line. */
        QString lyrics;
        bool operator< (const LyricsElement& rhs) const { return offset < rhs.offset; };
    };

    /** The iterator type of lyrics. */
    //typedef std::vector<LyricsElement>::const_iterator const_iterator;
    /** Return the first element of the lyrics. */
    //const_iterator begin() const { return m_element.begin(); };
    /** Return the end of the lyrics, usually an empty iterator. */
    //const_iterator end() const { return m_element.end(); };
    const LyricsElement& get(int id) const { return m_element.at(id); };
    const LyricsElement& operator[] (int id) const { return m_element.at(id); };
    /** Return the number of lines in the lyrics. */
    int size() const { return m_element.size(); };
    /** Return true if nothing stored in the class, otherwise return false. */
    bool isEmpty() const { return m_element.size() == 0; };
    /** Clear everything stored in the class. */
    void clear() { m_element.clear(); };
    /** Return track information of the lyrics. */
    const Track& getTrack() const { return m_track; };


    /** Find the line of the lyrics.
     * @return Return the line of lyrics which comes just after the offset. */
    //const_iterator find(const LyricsTime offset) const;
    int find(const LyricsTime offset) const;
    /** Load lyrics from a local file (.lrc), return true if the file is successful loaded. */
    bool loadFromFile(const QString& filename);
    void applyOffset(LyricsTime);
    void simplify();


private:
    /** The corresponding track information. */
    Track m_track;
    /** The offset of all time tag. */
    LyricsTime m_offset;
    /** The raw data of the lyrics. */
    std::vector<LyricsElement> m_element;

    /**
     * Parse a single line of .lrc file, and add the result into the list.
     * @param line Lyrics line with tags.
     * @param stream A QTextStream object, used to set encoding.
     * @return Unprocessed part of the line, usually the text of lyrics.
     */
    QString processLine(const QString& line, QTextStream& stream);
    QTextCodec* detectEncoding(QFile& file);
};

#endif
